package com.rules.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rules.admin.entity.SqlConfigModel;
import com.rules.admin.service.SqlConfigModelService;
import com.rules.admin.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping({"/sqlConfig"})
@Controller
public class SqlConfigController {
   @Autowired
   private SqlConfigModelService sqlConfigModelService;

   @RequestMapping(
      value = {"sqlConfig_page"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public IPage<SqlConfigModel> queryPage(String datasourceId, IPage<SqlConfigModel> page) {
      return this.sqlConfigModelService.queryPage(datasourceId, page);
   }

   @RequestMapping(
      value = {"sqlConfig_list/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getSqlConfigList(@PathVariable("id") String dataSourceId) {
      return ResultUtils.getSuccessResultData(this.sqlConfigModelService.selectListByDataSourceId(dataSourceId));
   }

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> addSqlConfig(@RequestBody SqlConfigModel sqlConfigModel) {
      this.sqlConfigModelService.save(sqlConfigModel);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getSqlConfig(@PathVariable("id") String id) {
      SqlConfigModel sqlConfigModel = (SqlConfigModel)this.sqlConfigModelService.getById(Long.parseLong(id));
      return ResultUtils.getSuccessResultData(sqlConfigModel);
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteSqlConfig(@PathVariable("id") String id) {
      this.sqlConfigModelService.removeById(Long.parseLong(id));
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateSqlConfig(@RequestBody SqlConfigModel sqlConfigModel) {
      this.sqlConfigModelService.save(sqlConfigModel);
      return ResultUtils.getSuccessResultData();
   }
}
